﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Master/Dialog.Master" AutoEventWireup="true"
    CodeBehind="PaymentView.aspx.cs" Inherits="Warehouse.Web.Purchase.PaymentView" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        .wrapper > * {
            width: 100%;
        }
        .infotable td:nth-child(1), .infotable td:nth-child(3) {
            width: 80px;
        }
        .infotable td:nth-child(2) {
            width: 160px;
        }
        .infotable td {
            padding: 0;
            height: 30px;
        }
        .infotable td > input[type="text"] {
            width: 100%;
        }
        p > * {
            float: right;
        }
        #detailstable td {
            padding: 0;
        }
        #detailstable input[disabled] {
            background: transparent;
        }
        #detailstable input[type="text"] {
            width: 100%;
            margin: 0;
            border: 0;
            height: 22px;
        }
        #detailstable .col-sn {
            width: 30px;
            padding: 0 5px;
        }
        #detailstable .col-account {
            text-align: center;
        }
        #detailstable .col-amount {
            width: 80px;
        }
        #detailstable .col-comments {
        }
        #detailstable .col-account select {
            width: 100%;
        }
        #detailstable .col-amount input {
            text-align: right;
            padding: 0 5px;
        }
    </style>

    <script type="text/javascript">
        var AllSupplier = {};

        function getData() {
            var bill = Util.getFields('.paymentbill');
            bill.SupplierID = bill.SupplierID - 0;
            if (!bill.SerialNumber) {
                alert("请输入单据编号");
                return;
            }
            if (!bill.SupplierID) {
                alert("请选择供货商");
                return;
            }
            if (!bill.Transactor) {
                alert("请选择经手人");
                return;
            }

            var details = [];
            var err = null;
            $('#detailstable tbody tr').each(function() {
                var d = Util.getFields(this);
                d.ID = d.ID - 0;
                d.AccountID = d.AccountID - 0;
                if (d.AccountID) {
                    d.Amount = d.Amount - 0;
                    if (isNaN(d.Amount) || d.Amount <= 0) {
                        err = "金额必须为正数";
                    } else {
                        details.push(d);
                    }
                }
            });
            if (err) {
                alert(err);
                return;
            }
            if (!details.length) {
                alert("请选择帐号");
                return;
            }
            return { Bill: bill, Details: details };
        }

        function add(action) {
            var data = getData();
            if (!data) return;

            Util.post("", { action: action, Bill: JSON.stringify(data.Bill), Details: JSON.stringify(data.Details) }, function(succeed, data, err) {
                if (succeed) {
                    alert('保存成功');
                    dialogCallback(true);
                } else {
                    alert(err);
                }
            }, { autoOverlay: true });
        }

        function save(action) {
            var data = getData();
            if (!data) return;

            Util.post("", { action: action, Bill: JSON.stringify(data.Bill), Details: JSON.stringify(data.Details) }, function(succeed, data, err) {
                if (succeed) {
                    alert('保存成功');
                    dialogCallback(true);
                } else {
                    alert(err);
                }
            }, { autoOverlay: true });
        }

        function invalid(action) {
            if (confirm("确认要作废此单据？")) {
                Util.post("", { action: action, ID: $('.paymentbill [field="ID"]').val() }, function(succeed, data, err) {
                    if (succeed) {
                        alert('作废成功');
                        dialogCallback(true);
                    } else {
                        alert(err);
                    }
                }, { autoOverlay: true });
            }
        }

        $(function() {
            AllSupplier = Util.loadObjects("#allsupplier", "ID");

            if (window.EditModel) {
                $('input[type="text"]').attr('readonly', 'readonly');
                $('select').attr('disabled', 'disabled');
                $('[field="Comments"],[field="Summary"]').removeAttr('readonly', 'readonly');
                $('#txtSupplier').val((AllSupplier[$('[field="SupplierID"]').val()] || { Name: '' }).Name);
            } else {
                $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });

                $('#txtSupplier').autocomplete({ autoFocus: true, delay: 10,
                    source: (function() {
                        var arr = [];
                        for (var k in AllSupplier) {
                            var p = AllSupplier[k];
                            arr.push({ label: p.Name + " " + p.Code, value: p.Name, data: p });
                        };
                        return arr;
                    })(),
                    select: function(event, ui) {
                        $('[field="SupplierID"]').val(ui.item.data.ID);
                    }
                }).bind('blur', function() {
                    var s = AllSupplier[$('[field="SupplierID"]').val()];
                    if (!s || s.Name != $(this).val()) {
                        $(this).val('');
                        $('[field="SupplierID"]').val('');
                    }
                });
            }
        });    
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Panel ID="pnlWrapper" runat="server" CssClass="wrapper">
        <table class="infotable paymentbill">
            <tr>
                <td>
                    <label>
                        录入日期</label>
                </td>
                <td>
                    <input type="hidden" field="ID" value="<%= this.PaymentBill.ID%>" />
                    <input type="text" class="datepicker" field="ReceiptDate" value="<%= this.PaymentBill.ReceiptDate.ToString("yyyy-MM-dd") %>" />
                </td>
                <td>
                    <label>
                        单据编号
                    </label>
                </td>
                <td>
                    <input type="text" field="SerialNumber" value="<%= this.PaymentBill.SerialNumber %>" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        经 手 人</label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlTransactor" runat="server" field="Transactor" DataValueField="LoginName"
                        DataTextField="Name">
                    </asp:DropDownList>
                </td>
                <td>
                    <label>
                        供 货 商
                    </label>
                </td>
                <td>
                    <input type="hidden" field="SupplierID" value="<%= this.PaymentBill.SupplierID %>" />
                    <input type="text" id="txtSupplier" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        摘 要
                    </label>
                </td>
                <td colspan="3">
                    <input type="text" field="Summary" value="<%= this.PaymentBill.Summary %>" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        附加说明
                    </label>
                </td>
                <td colspan="3">
                    <input type="text" field="Comments" value="<%= this.PaymentBill.Comments %>" />
                </td>
            </tr>
        </table>
        <table id="detailstable" class="datatable">
            <thead>
                <tr>
                    <th class="col-sn">
                        序号
                    </th>
                    <th class="col-account">
                        帐号
                    </th>
                    <th class="col-amount">
                        金额
                    </th>
                    <th class="col-comments">
                        备注
                    </th>
                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="rptDetails" runat="server" OnItemDataBound="rptDetails_ItemDataBound">
                    <ItemTemplate>
                        <tr>
                            <td class="col-sn">
                                <%# Container.ItemIndex+1 %>
                                <input type="hidden" field="ID" value="<%# Eval("ID")%>" />
                            </td>
                            <td class="col-account">
                                <asp:DropDownList ID="ddlAccount" runat="server" field="AccountID" DataTextField="Name"
                                    DataValueField="ID">
                                </asp:DropDownList>
                            </td>
                            <td class="col-amount">
                                <input type="text" field="Amount" value="<%# Eval("Amount")%>" />
                            </td>
                            <td class="col-comments">
                                <input type="text" field="Comments" value="<%# Eval("Comments")%>" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
        </table>
        <br />
        <p>
            <span>
                <uc:AuthorityControl ID="acAddPayment" runat="server" Action="AddPayment">
                    <input type="button" class="ui-button" value="保存" onclick="add('AddPayment');return false;" />
                </uc:AuthorityControl>
                <uc:AuthorityControl ID="acAddReceipt" runat="server" Action="AddReceipt">
                    <input type="button" class="ui-button" value="保存" onclick="add('AddReceipt');return false;" />
                </uc:AuthorityControl>
                <uc:AuthorityControl ID="acSavePayment" runat="server" Action="SavePayment">
                    <input type="button" class="ui-button" value="保存" onclick="save('SavePayment');return false;" />
                    <input type="button" class="ui-button" value="作废" onclick="invalid('InvalidPayment');return false;" />
                </uc:AuthorityControl>
                <uc:AuthorityControl ID="acSaveReceipt" runat="server" Action="SaveReceipt">
                    <input type="button" class="ui-button" value="保存" onclick="save('SaveReceipt');return false;" />
                    <input type="button" class="ui-button" value="作废" onclick="invalid('InvalidReceipt');return false;" />
                </uc:AuthorityControl>
                <input type="button" class="ui-button" value="取消" onclick="dialogCallback(false);return false;" />
            </span>
        </p>
    </asp:Panel>
</asp:Content>
